package models;

import java.util.*;

import javax.persistence.*;

import org.apache.commons.mail.EmailException;
import org.junit.Test;
import play.data.validation.Required;
import play.db.jpa.*;
import java.util.List;

/**
 * Comment model, Used to create comments that will be added to the idea.
 * 
 * @author Nada Hassan.
 */
@Entity
public class Comment extends play.db.jpa.Model {
	/**
	 * body of the comment.
	 * @author Nada Hassan
	 */
	@Required
	public String body;
	/**
	 * time of posting comment.
	 *  @author Nada Hassan
	 */
	public Date posttime;
	/**
	 * user that created the comment. many users can comment on the idea.
	 *  @author Nada Hassan
	 */
	@ManyToOne
	public User commentator;
	/**
	 * title of comment
	 * 
	 * @author Heba Khattab
	 */

	public String title;
	/**
	 * check wether the comment is reported as spam.
	 */
	public boolean spam;
	/**
	 * idea in which the comment is added. the idea can have many comments.
	 *  @author Nada Hassan
	 */
	@ManyToOne
	public Idea idea;
	/**
	 * The list of users that marked this comment as spam
	 * @author Shahir El Faramawy
	 */
	@JoinTable(name = "CommentSpammers")
	@ManyToMany
	public List<User> commentSpamReporters;

	/**
	 * * Default constructor
	 * 
	 * @param idea
	 *            idea in which comment is added to it.
	 * @param commentator
	 *            user that comments on the idea.
	 * @param body
	 *            body of the comment written.
	 * @param spam
	 *            wether the comment is reported as spam.
	 * @author Nada hassan
	 * @author Mai Selwanes only for the log statement
	 */

	public Comment(Idea idea, User commentator, String body) {
		this.commentator = commentator;
		this.body = body;
		this.idea = idea;
		this.posttime = new Date();
		this.spam = false;
		this.commentSpamReporters = new ArrayList<User>();
		Log log = new Log(idea.organization, commentator,
				" has commented on a the idea of title: " + idea.title);
		log.save();
	}

	/**
	 * * Default constructor
	 * 
	 * @param idea
	 *            idea in which comment is added to it.
	 * @param commentator
	 *            user that comments on the idea.
	 * @param body
	 *            body of the comment written.
	 * @param title
	 *            title of comment
	 * @param posttime
	 *            time in which comment is posted.
	 * @author Nada hassan
	 * @param spam
	 *            wether the comment is reported as spam.
	 * @author Heba Khattab
	 * @author Mai Selwanes only for the log statement
	 * 
	 */

	public Comment(Idea idea, User commentator, String body, String title) {
		this.commentator = commentator;
		this.body = body;
		this.idea = idea;
		this.title = title;
		this.posttime = new Date();
		this.spam = false;
		Log log = new Log(idea.organization, commentator,
				" has commented on a the idea of title: " + idea.title
						+ "with a comment of title: " + title);
		log.save();
	}

	/**
	 * marks a comment as spammed. An E-mail is sent to the organizer of 
	 * the topic that the comment belongs to and of reported person.
	 * 
	 * @param User
	 *            user The user who will spam the topic.
	 * @throws exception
	 * 			  for throwing an e-mail exception           
	 * 
	 * @author Shahir El Faramawy
	 * @author Mai Selwanes only for the log statement
	 */
	public void spamComment(User user) throws EmailException {
		this.commentSpamReporters.add(user);
		user.commentSpammed.add(this);
		this.save();
		user.save();
		String content = "has spammed comment" + this.body + "created by"
				+ this.commentator.name;
		ArrayList<User> subscribers = new ArrayList<User>();
		if (this.idea.organization != null) {
			subscribers.add(this.idea.organization.lead);
			Email mail = new Email(user.email, this.idea.organization.lead.email,
					"Spamming", user.name + "has spamed the comment " + this.body);
			mail.sendEmail();
		}
		if (this.idea.entity != null) {
			for (int counter = 0; counter < idea.entity.entityOrganizers.size(); counter++) {
				subscribers.add(this.idea.entity.entityOrganizers.get(counter));
				Email mail = new Email(user.email,
						this.idea.entity.entityOrganizers.get(counter).email,
						"Spamming", user.name + "has spamed the comment "
								+ this.body);
				mail.sendEmail();
			}
			subscribers.add(this.idea.entity.organization.lead);
			Email mail = new Email(user.email,
					this.idea.entity.organization.lead.email, "Spamming", user.name
							+ "has spamed the comment " + this.title);
			mail.sendEmail();
		}
		if (this.idea.topic != null) {
			for (int counter = 0; counter < idea.topic.entity.entityOrganizers
					.size(); counter++) {
				subscribers
						.add(this.idea.topic.entity.entityOrganizers.get(counter));
				Email mail = new Email(user.email,
						this.idea.topic.entity.entityOrganizers.get(counter).email,
						"Spamming", user.name + "has spamed the comment"
								+ this.body);
				mail.sendEmail();
			}
		}	
		user.sendNotificationWithType(content, subscribers, "Idea", this.idea.id);
		Log log = new Log(this.idea.organization, user,
				" has spammed the comment: " + this.body + "::::created by"
						+ this.commentator.name);
		log.save();
	}

}
